package com.wayne.blog.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wayne.blog.dto.CommentDTO;
import com.wayne.blog.entity.Comment;
import com.wayne.blog.entity.User;
import com.wayne.blog.mapper.CommentMapper;
import com.wayne.blog.service.CommentService;
import com.wayne.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评论服务实现类
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public void addComment(CommentDTO commentDTO) {
        // 获取当前登录用户
        User currentUser = userService.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("请先登录");
        }

        // 创建评论
        Comment comment = new Comment();
        comment.setArticleId(commentDTO.getArticleId());
        comment.setContent(commentDTO.getContent());
        comment.setUserId(currentUser.getId());
        comment.setParentId(commentDTO.getParentId());
        comment.setReplyUserId(commentDTO.getReplyUserId());
        comment.setLikeCount(0);
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());

        // 保存评论
        save(comment);
    }

    @Override
    @Transactional
    public void deleteComment(Long id) {
        // 获取当前登录用户
        User currentUser = userService.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("请先登录");
        }

        // 查询评论
        Comment comment = getById(id);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }

        // 检查权限
        if (!comment.getUserId().equals(currentUser.getId())) {
            throw new RuntimeException("无权删除此评论");
        }

        // 删除评论
        removeById(id);
    }

    @Override
    public List<Comment> getArticleComments(Long articleId) {
        // 查询顶级评论
        List<Comment> topComments = baseMapper.findTopCommentsByArticleId(articleId);

        // 查询每个顶级评论的子评论
        for (Comment topComment : topComments) {
            List<Comment> children = baseMapper.findChildrenByParentId(topComment.getId());
            topComment.setChildren(children);
        }

        return topComments;
    }

    @Override
    @Transactional
    public void likeComment(Long id) {
        // 获取当前登录用户
        User currentUser = userService.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("请先登录");
        }

        // 查询评论
        Comment comment = getById(id);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }

        // 增加点赞数
        comment.setLikeCount(comment.getLikeCount() + 1);
        updateById(comment);
    }

    @Override
    @Transactional
    public void unlikeComment(Long id) {
        // 获取当前登录用户
        User currentUser = userService.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("请先登录");
        }

        // 查询评论
        Comment comment = getById(id);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }

        // 减少点赞数
        if (comment.getLikeCount() > 0) {
            comment.setLikeCount(comment.getLikeCount() - 1);
            updateById(comment);
        }
    }
} 